package solver;

import static org.junit.Assert.*;

import java.awt.Color;
import org.junit.Test;

import de.dhbw.horb.inf2013.vierFarbenProblem.model.datatypes.AdjacencyListGraph;
import de.dhbw.horb.inf2013.vierFarbenProblem.model.datatypes.ColoredVertex;
import de.dhbw.horb.inf2013.vierFarbenProblem.model.datatypes.Graph;
import de.dhbw.horb.inf2013.vierFarbenProblem.model.datatypes.NoColorSetException;
import de.dhbw.horb.inf2013.vierFarbenProblem.model.datatypes.VertexNotSetException;
import de.dhbw.horb.inf2013.vierFarbenProblem.model.solver.BacktrackingIterativFourColorSolver;
import de.dhbw.horb.inf2013.vierFarbenProblem.model.solver.FourColorSolver;

/**
 * The Class SolverTest.
 */
public class SolverTest {
	//TODO all Vertexes Colored?
	
	/**
	 * Test example.
	 *
	 * @throws VertexNotSetException the vertex not set exception
	 * @throws NoColorSetException the no color set exception
	 */
	@Test
	public void testExample() throws VertexNotSetException, NoColorSetException {
		Graph graph = new AdjacencyListGraph();
		
		for(int i = 1; i <= 9; i++){
			ColoredVertex vertex = new ColoredVertex(i);
			graph.addVertex(vertex);
		}
		int[][] edges = new int[12][2];
		edges[0][0] = 8;
		edges[0][1] = 9;
		
		edges[1][0] = 1;
		edges[1][1] = 2;
		
		edges[2][0] = 1;
		edges[2][1] = 4;
		
		edges[3][0] = 2;
		edges[3][1] = 3;
		
		edges[4][0] = 2;
		edges[4][1] = 5;
		
		edges[5][0] = 3;
		edges[5][1] = 6;
		
		edges[6][0] = 4;
		edges[6][1] = 5;
		
		edges[7][0] = 4;
		edges[7][1] = 7;
		
		edges[8][0] = 5;
		edges[8][1] = 6;
		
		edges[9][0] = 5;
		edges[9][1] = 8;
		
		edges[10][0] = 6;
		edges[10][1] = 9;
		
		edges[11][0] = 7;
		edges[11][1] = 8;
		
		for(int[] edge: edges){
			graph.addEdge(edge[0], edge[1]);
		}
		
		
		FourColorSolver solver = new BacktrackingIterativFourColorSolver();
		
		solver.addColor(Color.black);
		solver.addColor(Color.white);
		solver.addColor(Color.red);
		solver.addColor(Color.blue);
		
		solver.setGraph(graph);
		solver.run();
		Graph coloredGraph = solver.getGraph();
		
		boolean result = true;

		System.out.println("TEST");
		for(int[] edge: edges){
			Integer key = edge[0];
			Integer value = edge[1];
			System.out.println(coloredGraph.getVertex(key).getColor());
			System.out.println(coloredGraph.getVertex(value).getColor());

			if(key != value){
				if(coloredGraph.getVertex(key).getColor().equals(coloredGraph.getVertex(value).getColor())){
					result = false;
				}
			}
		}
		
		
		assertEquals(true, result);
	}
	
	/**
	 * Test full4.
	 *
	 * @throws VertexNotSetException the vertex not set exception
	 * @throws NoColorSetException the no color set exception
	 */
	@Test
	public void testFull4() throws VertexNotSetException, NoColorSetException {
		Graph graph = new AdjacencyListGraph();
		
		for(int i = 1; i <= 4; i++){
			ColoredVertex vertex = new ColoredVertex(i);
			graph.addVertex(vertex);
		}
		int[][] edges = new int[6][2];
		edges[0][0] = 1;
		edges[0][1] = 2;
		
		edges[1][0] = 1;
		edges[1][1] = 3;
		
		edges[2][0] = 1;
		edges[2][1] = 4;
		
		edges[3][0] = 2;
		edges[3][1] = 3;
		
		edges[4][0] = 2;
		edges[4][1] = 4;
		
		edges[5][0] = 3;
		edges[5][1] = 4;
		
		for(int[] edge: edges){
			graph.addEdge(edge[0], edge[1]);
		}
		
		
		FourColorSolver solver = new BacktrackingIterativFourColorSolver();
		
		solver.addColor(Color.black);
		solver.addColor(Color.white);
		solver.addColor(Color.red);
		solver.addColor(Color.blue);
		
		solver.setGraph(graph);
		solver.run();
		Graph coloredGraph = solver.getGraph();
		
		boolean result = true;


		for(int[] edge: edges){
			Integer key = edge[0];
			Integer value = edge[1];
			System.out.println(coloredGraph.getVertex(key).getColor());
			System.out.println(coloredGraph.getVertex(value).getColor());

			if(key != value){
				if(coloredGraph.getVertex(key).getColor().equals(coloredGraph.getVertex(value).getColor())){
					result = false;
				}
			}
		}
		
		
		assertEquals(true, result);
	}
	
	/**
	 * Test selfie.
	 *
	 * @throws VertexNotSetException the vertex not set exception
	 * @throws NoColorSetException the no color set exception
	 */
	@Test
	public void testSelfie() throws VertexNotSetException, NoColorSetException {
		Graph graph = new AdjacencyListGraph();
		
		for(int i = 1; i <= 1; i++){
			ColoredVertex vertex = new ColoredVertex(i);
			graph.addVertex(vertex);
		}
		int[][] edges = new int[1][2];
		edges[0][0] = 1;
		edges[0][1] = 1;
		
		for(int[] edge: edges){
			graph.addEdge(edge[0], edge[1]);
		}
		
		
		FourColorSolver solver = new BacktrackingIterativFourColorSolver();
		
		solver.addColor(Color.black);
		solver.addColor(Color.white);
		solver.addColor(Color.red);
		solver.addColor(Color.blue);
		
		solver.setGraph(graph);
		solver.run();
		Graph coloredGraph = solver.getGraph();
		
		boolean result = true;


		for(int[] edge: edges){
			Integer key = edge[0];
			Integer value = edge[1];
			System.out.println(coloredGraph.getVertex(key).getColor());
			System.out.println(coloredGraph.getVertex(value).getColor());

			if(key != value){
				if(coloredGraph.getVertex(key).getColor().equals(coloredGraph.getVertex(value).getColor())){
					result = false;
				}
			}
		}
		
		
		assertEquals(true, result);
	}
	
	/**
	 * Test two separate vertexes.
	 *
	 * @throws VertexNotSetException the vertex not set exception
	 * @throws NoColorSetException the no color set exception
	 */
	@Test
	public void testTwoSeparateVertexes() throws VertexNotSetException, NoColorSetException {
		Graph graph = new AdjacencyListGraph();
		
		for(int i = 1; i <= 2; i++){
			ColoredVertex vertex = new ColoredVertex(i);
			graph.addVertex(vertex);
		}
		int[][] edges = new int[1][2];
		edges[0][0] = 1;
		edges[0][1] = 1;
		
		for(int[] edge: edges){
			graph.addEdge(edge[0], edge[1]);
		}
		
		
		FourColorSolver solver = new BacktrackingIterativFourColorSolver();
		
		solver.addColor(Color.black);
		solver.addColor(Color.white);
		solver.addColor(Color.red);
		solver.addColor(Color.blue);
		
		solver.setGraph(graph);
		solver.run();
		Graph coloredGraph = solver.getGraph();
		
		boolean result = true;


		for(int[] edge: edges){
			Integer key = edge[0];
			Integer value = edge[1];
			System.out.println(coloredGraph.getVertex(key).getColor());
			System.out.println(coloredGraph.getVertex(value).getColor());

			if(key != value){
				if(coloredGraph.getVertex(key).getColor().equals(coloredGraph.getVertex(value).getColor())){
					result = false;
				}
			}
		}
		
		
		assertEquals(true, result);
	}
	
	/**
	 * Test cycle.
	 *
	 * @throws VertexNotSetException the vertex not set exception
	 * @throws NoColorSetException the no color set exception
	 */
	@Test
	public void testCycle() throws VertexNotSetException, NoColorSetException {
		Graph graph = new AdjacencyListGraph();
		
		for(int i = 1; i <= 2; i++){
			ColoredVertex vertex = new ColoredVertex(i);
			graph.addVertex(vertex);
		}
		int[][] edges = new int[2][2];
		edges[0][0] = 1;
		edges[0][1] = 2;
		
		edges[1][0] = 2;
		edges[1][1] = 1;
		
		for(int[] edge: edges){
			graph.addEdge(edge[0], edge[1]);
		}
		
		
		FourColorSolver solver = new BacktrackingIterativFourColorSolver();
		
		solver.addColor(Color.black);
		solver.addColor(Color.white);
		solver.addColor(Color.red);
		solver.addColor(Color.blue);
		
		solver.setGraph(graph);
		solver.run();
		Graph coloredGraph = solver.getGraph();
		
		boolean result = true;


		for(int[] edge: edges){
			Integer key = edge[0];
			Integer value = edge[1];
			System.out.println(coloredGraph.getVertex(key).getColor());
			System.out.println(coloredGraph.getVertex(value).getColor());

			if(key != value){
				if(coloredGraph.getVertex(key).getColor().equals(coloredGraph.getVertex(value).getColor())){
					result = false;
				}
			}
		}
		
		
		assertEquals(true, result);
	}
	
	/**
	 * Test2.
	 *
	 * @throws VertexNotSetException the vertex not set exception
	 * @throws NoColorSetException the no color set exception
	 */
	@Test
	public void test2() throws VertexNotSetException, NoColorSetException {
		Graph graph = new AdjacencyListGraph();
		
		for(int i = 1; i <= 14; i++){
			ColoredVertex vertex = new ColoredVertex(i);
			graph.addVertex(vertex);
		}
		int[][] edges = new int[29][2];
		edges[0][0] = 1;
		edges[0][1] = 4;
		
		edges[1][0] = 1;
		edges[1][1] = 2;
		
		edges[2][0] = 1;
		edges[2][1] = 3;
		
		edges[3][0] = 1;
		edges[3][1] = 8;
		
		edges[4][0] = 2;
		edges[4][1] = 8;
		
		edges[5][0] = 3;
		edges[5][1] = 7;
		
		edges[6][0] = 3;
		edges[6][1] = 9;
		
		edges[7][0] = 3;
		edges[7][1] = 8;
		
		edges[8][0] = 4;
		edges[8][1] = 6;
		
		edges[9][0] = 4;
		edges[9][1] = 7;
		
		edges[10][0] = 4;
		edges[10][1] = 5;
		
		edges[11][0] = 5;
		edges[11][1] = 6;
		
		edges[12][0] = 5;
		edges[12][1] = 12;
		
		edges[13][0] = 6;
		edges[13][1] = 7;
		
		edges[14][0] = 6;
		edges[14][1] = 10;
		
		edges[15][0] = 7;
		edges[15][1] = 10;
		
		edges[16][0] = 8;
		edges[16][1] = 9;
		
		edges[17][0] = 9;
		edges[17][1] = 10;
		
		edges[18][0] = 9;
		edges[18][1] = 11;
		
		edges[19][0] = 10;
		edges[19][1] = 11;
		
		edges[20][0] = 10;
		edges[20][1] = 12;
		
		edges[21][0] = 10;
		edges[21][1] = 13;
		
		edges[22][0] = 10;
		edges[22][1] = 14;
		
		edges[23][0] = 11;
		edges[23][1] = 14;
		
		edges[24][0] = 13;
		edges[24][1] = 14;
		
		edges[25][0] = 13;
		edges[25][1] = 12;
		
		edges[26][0] = 3;
		edges[26][1] = 4;
		
		edges[27][0] = 6;
		edges[27][1] = 3;
		
		edges[28][0] = 5;
		edges[28][1] = 3;
				
		
		
		
		
		
		for(int[] edge: edges){
			graph.addEdge(edge[0], edge[1]);
		}
		
		
		FourColorSolver solver = new BacktrackingIterativFourColorSolver();
		
		solver.addColor(Color.black);
		solver.addColor(Color.white);
		solver.addColor(Color.red);
		solver.addColor(Color.blue);
		
		solver.setGraph(graph);
		solver.run();
		Graph coloredGraph = solver.getGraph();
		
		boolean result = true;

		System.out.println("TEST");
		for(int[] edge: edges){
			Integer key = edge[0];
			Integer value = edge[1];
			System.out.println(coloredGraph.getVertex(key).getColor());
			System.out.println(coloredGraph.getVertex(value).getColor());

			if(coloredGraph.getVertex(key).getColor().equals(coloredGraph.getVertex(value).getColor())){
				result = false;
			}
		}
		
		
		assertEquals(true, result);
	}
	
	/**
	 * Keine kante.
	 *
	 * @throws VertexNotSetException the vertex not set exception
	 * @throws NoColorSetException the no color set exception
	 */
	@Test
	public void keineKante() throws VertexNotSetException, NoColorSetException {
		Graph graph = new AdjacencyListGraph();
		
		for(int i = 1; i <= 2; i++){
			ColoredVertex vertex = new ColoredVertex(i);
			graph.addVertex(vertex);
		}
		int[][] edges = new int[0][2];
			
		
		
		for(int[] edge: edges){
			graph.addEdge(edge[0], edge[1]);
		}
		
		
		FourColorSolver solver = new BacktrackingIterativFourColorSolver();
		
		solver.addColor(Color.black);
		solver.addColor(Color.white);
		solver.addColor(Color.red);
		solver.addColor(Color.blue);
		
		solver.setGraph(graph);
		solver.run();
		Graph coloredGraph = solver.getGraph();
		
		boolean result = true;

		System.out.println("TEST");
		for(int[] edge: edges){
			Integer key = edge[0];
			Integer value = edge[1];
			System.out.println(coloredGraph.getVertex(key).getColor());
			System.out.println(coloredGraph.getVertex(value).getColor());

			if(coloredGraph.getVertex(key).getColor().equals(coloredGraph.getVertex(value).getColor())){
				result = false;
			}
		}
		
		
		assertEquals(true, result);
	}
	
	/**
	 * Test bipartiter graph nicht planar.
	 *
	 * @throws VertexNotSetException the vertex not set exception
	 * @throws NoColorSetException the no color set exception
	 */
	@Test
	public void testBipartiterGraphNichtPlanar() throws VertexNotSetException, NoColorSetException {
		Graph graph = new AdjacencyListGraph();
		
		for(int i = 1; i <=6; i++){
			ColoredVertex vertex = new ColoredVertex(i);
			graph.addVertex(vertex);
		}
		int[][] edges = new int[9][2];
		
		edges[0][0]=1;
		edges[0][1]=4;
		
		edges[1][0]=1;
		edges[1][1]=5;
		
		edges[2][0]=1;
		edges[2][1]=6;
		
		edges[3][0]=2;
		edges[3][1]=4;
		
		edges[4][0]=2;
		edges[4][1]=5;
		
		edges[5][0]=2;
		edges[5][1]=6;
		
		edges[6][0]=3;
		edges[6][1]=4;
		
		edges[7][0]=3;
		edges[7][1]=5;
		
		edges[8][0]=3;
		edges[8][1]=6;
				
		for(int[] edge: edges){
			graph.addEdge(edge[0], edge[1]);
		}
		
		
		FourColorSolver solver = new BacktrackingIterativFourColorSolver();
		
		solver.addColor(Color.black);
		solver.addColor(Color.white);
		solver.addColor(Color.red);
		solver.addColor(Color.blue);
		
		solver.setGraph(graph);
		solver.run();
		Graph coloredGraph = solver.getGraph();
		
		boolean result = true;

		System.out.println("TEST");
		for(int[] edge: edges){
			Integer key = edge[0];
			Integer value = edge[1];
			System.out.println(coloredGraph.getVertex(key).getColor());
			System.out.println(coloredGraph.getVertex(value).getColor());

			if(coloredGraph.getVertex(key).getColor().equals(coloredGraph.getVertex(value).getColor())){
				result = false;
			}
		}
		
		
		assertEquals(true, result);
	}
}


